const utils=require('utils')
Page({
    data:{
        meters:0,
        seconds:0,
        longitude:0,
        latitude:0,
        running:false,
        interval:1000,
        markers:[],
        times:[],
    },
    run(){//更改记录状态
        this.setData({
            running:!this.data.running
        })
    },
    record(){//记录红点-每秒调用一次
        if(!this.data.running){
            return
        }
        this.setData({//更新时间
            seconds:this.data.seconds + this.data.interval/1000
        })
        wx.getLocation({
          type:'gcj02',
        }).then(res=>{
            let newMarker={
                latitude:res.latitude,
                longitude:res.longitude,
                iconPath:'images/redPoint.png',
                width:10,
                height:10,
            }
            let pace=0
            let times=this.data.times
            let markers=this.data.markers
            if(this.data.markers.length>0){
                let lastmarker=this.data.markers.slice(-1)[0]
                pace=utils.getDistance(//计算离上一个点的距离
                    lastmarker.longitude,
                    lastmarker.latitude,
                    newMarker.longitude,
                    newMarker.latitude
                )
                if(pace>15){
                    markers.push(newMarker)
                    times.push(this.data.seconds)
                    console.log(times)
                }else{
                    pace=0
                }
            }else{
                markers.push(newMarker)
                times.push(this.data.seconds)
            }
            this.setData({
                longitude:res.longitude,
                latitude:res.latitude,
                markers,
                times,
                meters:this.data.meters+pace
            })
        })
    },
    clear(){
        this.setData({
            markers:[],
            meters:0,
            seconds:0
        })
    },
    playback(){//回放按钮
        let that=this
        wx.getStorage({
            key:'markers',
            success(e){
                that.setData({
                    tempMarkers:e.data[0],
                    meters:e.data[1],
                    seconds:e.data[2],
                    times:e.data[3]
                })
            },
            complete(){that.replay()}
        })
    },
    save(){
        let data=[
            this.data.markers,
            this.data.meters,
            this.data.seconds,
            this.data.times
                ]
        wx.setStorage({
            data,
            key:'markers',
            success(){
                wx.showToast({title: '保存成功',})
            },
            fail(){
                wx.showToast({
                  title: '保存失败',
                  icon:'error'
                })
            }
        })
    },
    replay(){//回放功能体
        let that=this
        let markers=that.data.tempMarkers
        let meters=that.data.meters
        let time=that.data.times
        let i=markers.length
        let reMeters=meters/i
        let seconds=[],reMarkers=[]
        meters=0
        if(markers.length>0){
            setInterval(() => {
                if(i>0){
                    reMarkers.push(markers.shift())
                    meters+=reMeters
                    seconds=time.shift()
                    that.setData({
                        markers:reMarkers,seconds,meters
                    })
                    i--
                }else{return}
            }, 50);
        }
    },
    onLoad(){
        wx.getLocation({
            type:'gcj02'
        }).then(res=>{
            let{longitude,latitude}=res
            this.setData({
                longitude,latitude
            })
        })
        setInterval(this.record,this.data.interval);
    }
})